﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using System.ComponentModel;
using Sector7.Framework.Consts;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Controls
{
    /// <summary>
    /// DataElementTableControl.xaml 的交互逻辑
    /// </summary>
    public partial class DataGroupTableControl : UserControl
    {
        public static readonly DependencyProperty DataGroupTypeProperty = DependencyProperty.Register("DataGroupType", typeof(int), typeof(DataGroupTableControl));

        [Description("获取或设置数据分组类型（0 - 全部； 1 - 数据元； 2 - 数据元值域； 3 - 数据模板）")]
        [Category("其他")]
        public int DataGroupType
        {
            get { return Convert.ToInt32(this.GetValue(DataGroupTypeProperty)); }
            set { this.SetValue(DataGroupTypeProperty, value); }
        }

        public DataGroupTableControl()
        {
            InitializeComponent();
        }

        private List<S7_DataGroup> m_dataGroups = null;

        private void GetData()
        {
            m_dataGroups = DataGroupServiceAgent.GetAll(DataGroupType);
        }

        private void InitializeWindow()
        {
            if (m_dataGroups != null)
            {
                tbcMain.Items.Clear();

                foreach (var dataGroup in m_dataGroups)
                {
                    UserControl itemContent = null;
                    switch (DataGroupType)
                    {
                        case C_DataGroupType.DATA_ELEMENT:
                            itemContent = new DataElementTabItem();
                            itemContent.SetValue(DataElementTabItem.DataGroupProperty, dataGroup);
                            break;
                        case C_DataGroupType.VALUE_DOMAIN:
                            itemContent = new ValueDomainTabItem();
                            itemContent.SetValue(ValueDomainTabItem.DataGroupProperty, dataGroup);
                            break;
                        case C_DataGroupType.DATA_TEMPLATE:
                            break;
                        default:
                            Log.WriteLine(LogSeverityType.WRN, "无效分组类型：[{0}]！", dataGroup.GroupType);
                            continue;
                    }

                    TabItem tabItem = new TabItem();
                    tabItem.Header = dataGroup.GroupIdentifier;
                    tabItem.ToolTip = dataGroup.GroupName;
                    tabItem.Content = itemContent;

                    tbcMain.Items.Add(tabItem);
                }
            }
            else
            {
                Log.WriteLine(LogSeverityType.WRN, "DAD", "数据分组为空！");

                //**************** as below part is able to design easily, it should no been work in real runtime. ****************//
                grdTabItem.Children.Clear();

                UserControl itemContent = null;
                switch (DataGroupType)
                {
                    case C_DataGroupType.DATA_ELEMENT:
                        itemContent = new DataElementTabItem();
                        break;
                    case C_DataGroupType.VALUE_DOMAIN:
                        itemContent = new ValueDomainTabItem();
                        break;
                    case C_DataGroupType.DATA_TEMPLATE:
                        break;
                    default:
                        break;
                }

                grdTabItem.Children.Add(itemContent);
                //*****************************************************************************************************************//:~
            }
        }

        void tabItem_Initialized(object sender, EventArgs e)
        {
            Console.WriteLine((sender as TabItem).ToolTip);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();

            InitializeWindow();
        }
    }
}
